home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr44 / speller2.zip / SPELLER.CPP < prev    next >
C/C++ Source or Header  |  1995-02-13  |  11KB  |  628 lines

  1. #include <conio.h>
  2. #include <iostream.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <io.h>
  7. #include <ctype.h>
  8. #include <conio.h>
  9. #if defined (_MSC_VER) || defined (__WATCOMC__)
  10. #include <graph.h>
  11. #endif
  12. #include "spell.hpp"
  13. const int MAXWORD=40,
  14.    MAXLINE =81;
  15. char line[MAXLINE];
  16. char separator[MAXLINE];
  17.  
  18. char *iptr=NULL; //ptr to word;
  19. int ypos;
  20. int xbegin,xend;
  21.  
  22. FILE *fp,*fpout;
  23. char str[132];
  24. char *wptr;
  25. int cline;
  26. int cword;
  27.  
  28. int Abort=0; // abort flag
  29. int Ignore=0;
  30.  
  31. //#define SPELL EmsSpell
  32. #define SPELL CXmsSpell
  33. //#define SPELL XmsSpell
  34. //#define SPELL DiskSpell
  35.  
  36. SPELL *sp;
  37. ////////////////////////////////////////////////
  38. //
  39. //
  40. ///////////////////////////////////////////////////
  41.  
  42.  
  43. int isletter(char a){return (isalpha(a) || a=='-');}
  44. #if defined (__SC__)
  45.         char static tword[MAXWORD];
  46.  
  47. #define WHITE 15//_WHITE
  48. #define BLACK 0 //_BLACK
  49. #define LIGHTGRAY 7//_GRAY
  50. char wx1=0,wy1=0,wx=79,wy=24;
  51. char attr=7,fore,back;
  52. char curx,cury;
  53. void    clrscr()
  54.     {
  55.     char wx2=wx1+wx;
  56.     char wy2=wy1+wy;
  57.   _asm{
  58. /*    mov ah,0;
  59.     mov al,3;
  60.     int 0x10;
  61.     mov ah,5;
  62.     mov al,0;
  63.     int 0x10;*/
  64.    mov ah,6 ;
  65.    mov al,0  ;
  66.    mov bh,0;
  67.    mov ch,wy1  ;
  68.    mov cl,wx1  ;
  69.    mov dh,wy2   ;
  70.    mov dl,wx2;
  71.    int 0x10   ;
  72.  
  73.   }
  74.   }
  75.  void    textcolor(int color)
  76.  {
  77.  fore=color &0x0f;
  78.  attr=fore+back;
  79.  }
  80.  void    textbackground(int color)
  81.   {
  82.  if(color == WHITE )
  83.   color= LIGHTGRAY;
  84. back=char(color <<4) & 0xf0;
  85. attr=fore+back;
  86.  
  87.   }
  88. void    clreol()
  89.     {
  90.     /*
  91.   _asm{
  92.    mov ah,7
  93.    mov al,0
  94.    mov ch,24
  95.    mov cl,79
  96.    mov dh,0
  97.    mov dl,0
  98.    int 10h
  99.   }   */
  100.   }
  101. void    gotoxy(int x,int y)
  102. {
  103. char c_x=x-1+wx1;
  104. char c_y=y-1+wy1;
  105. curx=x;
  106. cury=y;
  107. asm { mov ah,2;
  108.    mov bh,0;
  109.    mov dh,c_y;
  110.   mov dl,c_x;
  111.    int 0x10;
  112.   }
  113.  
  114. }
  115. lf()
  116. {
  117.     if(cury > wy)
  118.     {
  119.     // scroll
  120.     char wx2=wx1+wx,wy2=wy1+wy;
  121.     _asm {
  122.    mov ah,6 ;
  123.    mov al,1  ;
  124.    mov bh,0;
  125.    mov ch,wy1  ;
  126.    mov cl,wx1  ;
  127.    mov dh,wy2   ;
  128.    mov dl,wx2;
  129.    int 0x10   ;
  130.    }
  131.     }
  132.     else
  133.     cury++;
  134. }
  135. void  cputs(char *msg)
  136. {
  137.  
  138. char chr;
  139.   while(*msg ) {
  140.     chr=*msg++;
  141.     if(chr == '\r'){
  142.     gotoxy(1,cury);
  143.     continue;
  144.     }
  145.     if(chr == '\n')
  146.     { lf();
  147.   gotoxy(curx,cury);
  148.   continue;
  149.     }
  150.   _asm{
  151.    mov ah,0x09;
  152.    mov al,chr ;
  153.    mov bh,0;
  154.    mov bl,attr;
  155.    mov cx,1 ;
  156.    int 0x10;
  157.   }
  158.  curx++;
  159.     if(curx >wx)
  160.    {curx=1;
  161.   lf();
  162.     }
  163.  gotoxy(curx,cury);
  164. }
  165.  
  166.  
  167. }
  168. void    window(int x1,int y1,int x2,int y2)
  169. {
  170. wx1=x1-1;
  171. wy1=y1-1;
  172. wx=x2-x1;
  173. wy=y2-y1;
  174. gotoxy(1,1);
  175. //clrscr()
  176.  
  177. }
  178. int wherex()
  179. {
  180.   char pos;
  181. asm { mov ah,3
  182.       MOV BH,0;
  183.    int 0x10;
  184.     mov pos,dl
  185.   }
  186.  
  187. return int(pos)+1-wx1;
  188. }
  189. int wherey()
  190. {
  191.   char pos;
  192. asm { mov ah,3
  193.       MOV BH,0;
  194.    int 0x10;
  195.     mov pos,dh
  196.   }
  197.  
  198. return int(pos)+1-wy1;
  199. }
  200.  
  201.  
  202. #endif
  203.  
  204. #if defined (_MSC_VER)  || defined( __WATCOMC__ )
  205. #define WHITE 15//_WHITE
  206. #define BLACK 0 //_BLACK
  207. #define LIGHTGRAY 7//_GRAY
  208. void    clrscr()
  209.     {
  210.    _clearscreen( _GWINDOW );
  211.  
  212.     }
  213.  
  214.  void    textcolor(int color)
  215.  {
  216. _settextcolor(color);
  217.  }
  218.  void    textbackground(long color)
  219. {
  220. _setbkcolor(color);
  221. }
  222. void    clreol()
  223.     {
  224. //   _clearscreen( _GCLEARSCREEN );
  225.  
  226.     }
  227.  
  228. void    cputs(char *msg)
  229. {
  230.  _outtext(msg);
  231. }
  232. void    gotoxy(int x,int y)
  233. {
  234. _settextposition(y,x);
  235.  
  236.  
  237. }
  238. void    window(int x1,int y1,int x2,int y2)
  239. {
  240. _settextwindow(y1,x1,y2,x2);
  241. }
  242. int wherex()
  243. {
  244.    struct rccoord rc;
  245.    rc = _gettextposition();
  246.      return rc.col;
  247. }
  248. int wherey()
  249. {
  250.    return (_gettextposition()).row;
  251. }
  252.  
  253. #endif
  254.  
  255. char *getword()   /* get the next word in the input stream */
  256.  
  257. {
  258.     register char *wp;        /* ptr into word */
  259. //    register int len;        /* length of word */
  260.   static char  word[MAXWORD];    /* word to return */
  261.  
  262.  
  263. nexline:
  264.     /* time to read a new line? */
  265.     if (iptr == NULL || *iptr == 0) {
  266.    if(cline > 0)
  267.     {
  268.     // write line
  269.       fputs(line,fpout);
  270.       putc('\n',fpout);
  271.     }
  272.  
  273.     if (fgets(line, MAXLINE - 1, fp) == NULL) {
  274.       fclose(fp);
  275.       return NULL;
  276.  
  277.         }
  278. // remove newline
  279.     line[strlen(line)-1]= '\0';
  280.         iptr = line;
  281.     cline++;
  282.     cputs("\r\n");
  283.    ypos=wherey();
  284.     cputs(line);
  285.     }
  286.  
  287.     /* skip off leading non-alphas */
  288.     while (*iptr && !isletter(*iptr))
  289.         iptr++;
  290.  
  291.     if (*iptr == 0)
  292.          goto nexline;
  293.     /* and now set the word up */
  294.      xbegin=int(iptr-line);
  295.   wp = word;
  296.     while (*iptr && (isletter(*iptr) || *iptr == '\''))
  297.         *wp++ = *iptr++;
  298.  
  299.  
  300.     xend=int(iptr-line);
  301.  
  302.  
  303.       /* dump trailing 's && 'd */
  304.             if (*(wp-2) == '\'') {
  305.       if (*(wp-1) == 's' || *(wp-1) == 'S' ) // || *(wp-1) == 'd'*(wp-1) == 'D'))
  306.     wp -= 2;}
  307.  
  308.     *wp = 0;
  309.     if(word[0] == '\0') return NULL;
  310.   return(word);
  311. }
  312.  
  313. void change(char *word)
  314. {
  315.    // xbegin
  316.   int wlen=strlen(word);
  317.   int llen=strlen(line);
  318.    int oldlen =xend -xbegin ;
  319.    if(wlen > oldlen)
  320.      {
  321.       // insert blanks
  322.    memmove(line+xbegin+wlen+1,line+xend,llen-xend);  
  323.    memcpy(line+xbegin,word,wlen);
  324.    memset(line+xbegin+wlen ,' ',1);
  325.      }
  326.    else
  327.       {
  328.        memcpy(line+xbegin,word,wlen);
  329.        memset(line+xbegin+wlen ,' ',oldlen-wlen);
  330.       }
  331.  
  332. }
  333.  
  334. void selectWord(char *word)
  335. {
  336.  textcolor(BLACK);
  337.  textbackground(WHITE);
  338.  if(word){
  339.  gotoxy(xbegin+1,ypos);
  340.  cputs(word);
  341.          }
  342. }
  343. void unselectWord(char *word)
  344. {
  345.  textcolor(LIGHTGRAY);
  346.  textbackground(BLACK);
  347.  if(word){
  348.  gotoxy(xbegin+1,ypos);
  349.  cputs(word);
  350.          }
  351.  
  352. }
  353. int  editline(char *wrd)
  354. {
  355.   char ch;
  356.   int i=0;
  357. //  int wlen= strlen(wrd);
  358.   int row=wherey();
  359.   int result;
  360.  textcolor(BLACK);
  361.  textbackground(WHITE);
  362.   clreol();
  363.   cputs(wrd);
  364.   gotoxy(1,row);
  365.       while (!Abort)
  366.       {
  367.         ch=getch();
  368.     if(ch == 27 )
  369.         {
  370.       result= 0;
  371.       break;
  372.       //s_word[0]= '\0';
  373.     }
  374.     else if(ch == 13 )
  375.         {
  376.     change(wrd);
  377.       result= 1;
  378.       break;
  379.         }
  380.        else if( ch == 0)
  381.        {
  382.         ch=getch();
  383.           switch(ch)
  384.           {
  385.         case 77:
  386.              if( i < 29 )
  387.          i++;
  388.             break;
  389.             case 75:
  390.              if( i > 0)
  391.              i--;
  392.         break;
  393.             case 67 :  // Ignore All
  394.               Ignore=1;
  395.         ungetch(27);
  396.             break;
  397.             case 68 :  // Abort
  398.           Abort = 1;
  399.           result =0;
  400.         break;
  401.             case 66 :  // Ignore  //F8
  402.               ungetch(27);
  403.               break;
  404.         case 65 :  // Add  //F7
  405.               sp->add(wrd,sp->tempDic);
  406.               ungetch(27);
  407.               break;
  408.             case 64 :  // change  //F6
  409.             change(wrd);
  410.               ungetch(27);
  411.        break;
  412.  
  413.       }
  414.        }
  415.        else if( isascii(ch))
  416.     {
  417.           wrd[i] = ch;
  418.      #if (__SC__)
  419.      putc(ch,stdout);
  420.     #else
  421.           putch(ch);
  422.     #endif
  423.           if(i < 29)
  424.           i++;
  425.         }
  426.        gotoxy(1+i,row);
  427.  
  428.       }
  429.  
  430.  textcolor(LIGHTGRAY);
  431.  textbackground(BLACK);
  432. return result;
  433. }
  434.  
  435.  int main(int argc,char **argv)
  436.  {
  437. int mode;
  438. char ch;
  439. char s_word[30]; // suggested word
  440. char *word;
  441. int savex,savey;
  442. /////////////////////////////////////////////////
  443. if (argc!=2)
  444. {
  445. printf("\n\rThe Speller v1.0 Demo Program\n\r Copyright 1995, (c) The Yucatan Electronic Assembly Company\n\r");
  446. printf("\n\r speller      - Checks the spelling of a list of words and ouputs to spelled.ok\n\r");
  447. printf("\n\n  Syntax: \n\r    speller infile \n\r\n\r infile  - The file name of the text to be spell checked");
  448. exit(0);
  449. }
  450.  
  451. if((fp=fopen(argv[1],"r")) == NULL)
  452. {
  453. printf("Error in opening file %s\n",argv[1]);
  454. exit(8);
  455. }
  456.  
  457. fpout=fopen("spelled.ok","w");
  458.  
  459. cout <<"loading dictionary...";
  460. sp =new  SPELL;
  461.  
  462. cout <<"\r Done                " <<endl;
  463. if(sp->getLastError())
  464. {
  465. delete sp;
  466. cout <<"\r Error Loading Dictionary";
  467. exit(0);
  468. }
  469.  
  470. mode=1;
  471. s_word[0]=0;
  472. clrscr();
  473.  
  474. memset(separator,'─',80);
  475. gotoxy(1,16);
  476. cputs(separator);
  477. gotoxy(6,16);
  478. cputs("┤ Suggestion(s)) ├");
  479. gotoxy(26,16);
  480. cputs("F6-replace F7-Add F8-Ignore F9-Ignore All F10-Abort");
  481. window(1,1,80,15);
  482.  
  483.  
  484. cline=cword=0;
  485.   sp->SetLevel (COMPLEX_MATCH);// |TYPOS_MATCH );
  486.   while((word=getword())!= NULL && !Abort)
  487.     {
  488.     cword++;
  489.         if(sp->check(word) == 0) {
  490.           if(mode) {
  491.         selectWord(word);
  492.          savex=wherex();
  493.          savey=wherey();
  494.          textcolor(LIGHTGRAY);
  495.     textbackground(BLACK);
  496.          window(1,17,80,25);
  497.     clrscr();
  498.   cputs("Searching ...");
  499.     #if defined (__SC__)
  500.      strcpy(tword,word);
  501.         sp->suggest(tword);
  502.     #else
  503.             sp->suggest(word);
  504.     #endif
  505.     clrscr();
  506.         if(sp->suggestion->count > 0 ){
  507.         int wlen=strlen(word)+5;
  508.         int ncol= 80 / wlen;
  509.         int scount = sp->suggestion->count;
  510.         for(int i=0;i < scount; i++)
  511.         {
  512.          gotoxy(1+(i % ncol)*wlen,wherey());
  513.     cputs(sp->suggestion->get(i)->name);
  514.          if(i % ncol ==( ncol-1))
  515.          cputs("\r\n");
  516.          }
  517.   int row=0,col=0;
  518.   i=0;
  519.   gotoxy(1,1);
  520.   while (!Abort)
  521.   {
  522.   ch=getch();
  523.   if(ch == 0)
  524.      {
  525.       ch=getch();
  526.         switch(ch)
  527.         {
  528.     case 77 :   // right
  529.     if(i < scount-1)
  530.     i++;
  531.  
  532.  
  533.     break;
  534.     case 75 :   // left
  535.     if(i > 0 )
  536.     i--;
  537.     break;
  538.     case 72 :   // up
  539.     if(i >= ncol)
  540.     i-=ncol;
  541.  
  542.     break;
  543.     case 80 :   // down
  544.     if( i+ncol < scount)
  545.     i+=ncol;
  546.     break;
  547.     case 68 :  // Abort          // F10
  548.     Abort = 1;
  549.          ungetch(27); // put back esc
  550.     break;
  551.          case 67 :  // Ignore All   //F9
  552.          sp->ignoreDic->insert(word);
  553.          goto endfor; // put back esc
  554.          case 66 :  // Ignore  //F8
  555.          goto endfor;
  556.          case 65 :  // Add  //F7
  557.          sp->add(word,sp->tempDic);
  558.          goto endfor;
  559.          case 64 :  // change  //F6
  560.          change(sp->suggestion->get(i)->name);
  561.          goto endfor;
  562.     default:
  563.     break;
  564.         }
  565.         col= i% ncol;
  566.         row = i/ ncol;
  567.         gotoxy(1+col*wlen,1+row);
  568.  
  569.      }
  570.   else if(ch == 13) // return
  571.      {
  572.         strcpy(s_word,sp->suggestion->get(i)->name);
  573.         // ok
  574.         break;
  575.      }
  576.      else if( ch == 27) //escape
  577.      {
  578.         strncpy(s_word,word,29);
  579.         // cancel
  580.         break;
  581.  
  582.      }
  583.   }
  584.   //if(Abort) break;
  585.   }
  586.     else
  587.     {cputs("No suggestion found");
  588. //       sp->add(word,sp->tempDic);
  589.         strncpy(s_word,word,29);
  590.          }
  591.   // edit suggestion
  592.          gotoxy(1,9);
  593.          if(editline(s_word))
  594.          {
  595.           //change text
  596.          }
  597.          else     {
  598.          if(Ignore)
  599.          {
  600.           Ignore = 0;
  601.          sp->ignoreDic->insert(word);
  602.          }
  603.          }
  604.         // cout <<endl;
  605. endfor:
  606.         window(1,1,80,15);
  607.         gotoxy(savex,savey);
  608.         unselectWord(word);
  609.         }
  610.         else  // no suggestions
  611.         sp->tempDic->insert(word);
  612.         }
  613. }
  614. clrscr();
  615.  
  616.      if( sp->tempDic->modified  && !Abort)
  617.     {
  618.   sp->tempDic->save("temp.dic");
  619.   cerr << "Total misspelled word count = " << sp->tempDic->count << endl;
  620.     }
  621.  
  622. delete sp;
  623.  
  624.         return 0;
  625.  
  626.  }
  627.  
  628.